package com.man.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.man.bean.People;
import com.util.JdbcUtil;

public class PeopleDao {
	QueryRunner qr = new QueryRunner(JdbcUtil.getDataSource());
	
	//注册账号
	public int savePeople(People peo) {
		int num = 0;
		String sql="insert into d_peo (account,`name`,sex,pro,cls) VALUES(?,?,?,?,?)"; ; 
		try {
			num=qr.update(sql,peo.getAccount(),peo.getName(),peo.getSex(),peo.getPro(),peo.getCls());
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return num;
	}
		public List <People> showPeo() {
			List <People> course = new ArrayList<People>();//定义列表
		
			String sql = "select account,`name`,sex,pro,cls from d_peo";
			try {
				course = qr.query(sql, new BeanListHandler <People>(People.class));
			}catch(SQLException e) {
				e.printStackTrace();
			}
			return course;
		}
	
	//查找单个
		public List <People> showPeo(String ac) {
			List <People> course = new ArrayList<People>();//定义列表
		
			String sql = "select account,`name`,sex,pro,cls from d_peo where account like \"%\"? \"%\" ";
			try {
				course = qr.query(sql, new BeanListHandler <People>(People.class),ac);
			}catch(SQLException e) {
				e.printStackTrace();
			}
			return course;
		}
		
		
	//更改学生
	public int	changePeo(People people,String ac) {
			int num = 0;
			String sql="UPDATE d_peo set account=?,`name`=?,sex=?,pro=?,cls=? where account=?"; ; 
			try {
				num=qr.update(sql,people.getAccount(),people.getName(),people.getSex(),people.getPro(),people.getCls(),ac);
			}catch(SQLException e) {
				e.printStackTrace();
			}
			return num;
	}
	//删除
	public int delPeople(String ac) {
		int num = 0;
		String sql="DELETE FROM d_peo where  account=?"; 
		try {
			num=qr.update(sql,ac);
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return num;
	}
	
	

}
